package com.zxbit.uangstore.constant;

import android.os.Build;
import android.text.TextUtils;

import com.appsflyer.AppsFlyerLib;
import com.google.gson.Gson;
import com.zxbit.uangstore.BuildConfig;
import com.zxbit.uangstore.CashLoanApplication;
import com.zxbit.uangstore.cache.Acache;
import com.zxbit.uangstore.cache.AcacheKeys;
import com.zxbit.uangstore.utils.DeviceUtils;
import com.zxbit.uangstore.utils.ScreenUtils;
import com.zxbit.uangstore.utils.UniqueUtils;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.RequestBody;


/**
 * 公共参数生成工厂
 * Created by wangjing on 2017/5/16.
 */
public class ParamsFactory {

    private static Map<String, String> unChangeCommonParams;
    private static Map<String,String>installCommonParams;

    private String language = Locale.getDefault().getLanguage();
//    private String language = "in";

    public static RequestBody getCommonParams(Map<String, Object> formParams) {
        checkUnChangeParams();

        Map<String, Object> params = new HashMap<>();
        params.put("channel", BuildConfig.channel);
        params.put("commonParam", unChangeCommonParams);
        if (formParams != null) {

            params.putAll(formParams);
        }
        return FormBody.create(MediaType.parse("application/json"), new Gson().toJson(params));
    }

    public static RequestBody getInstallAppParams(Map<String, Object> formParams) {
        checkInstallParams();

        Map<String, Object> params = new HashMap<>();
        params.put("channel", BuildConfig.channel);
        params.put("commonParam", unChangeCommonParams);
        if (formParams != null) {

            params.putAll(formParams);
        }
        return FormBody.create(MediaType.parse("application/json"), new Gson().toJson(params));
    }

    public static String getCommonParamsStr(Map<String, Object> formParams) {
        checkUnChangeParams();

        Map<String, Object> params = new HashMap<>();
        params.put("channel", BuildConfig.channel);
        params.put("commonParam", unChangeCommonParams);
        if (formParams != null) {

            params.putAll(formParams);
        }
        return new Gson().toJson(params);
    }

    public static RequestBody getParams(Map<String, Object> formParams) {
        Map<String, Object> params = new HashMap<>();
        params.put("channel", BuildConfig.channel);
        if (formParams != null) {

            params.putAll(formParams);
        }
        return FormBody.create(MediaType.parse("application/json"), new Gson().toJson(params));
    }


    /**
     * 检查是否给不变的公共参数赋值了
     *
     * @return
     */
    private static void checkUnChangeParams() {
        if (unChangeCommonParams == null || unChangeCommonParams.size() == 0) {
            unChangeCommonParams = new HashMap<>();
            // 客户端软件版本
            unChangeCommonParams.put("cv", DeviceUtils
                    .getVersionNameAndVersionCode(CashLoanApplication.getContext()));
            // 操作系统
            unChangeCommonParams.put("os", DeviceUtils.getPlatform());
            // 操作系统版本
            unChangeCommonParams.put("ov", Build.VERSION.RELEASE);
            // 屏幕尺寸
            unChangeCommonParams.put("sc", ScreenUtils.genScreenResolution());
            // 包名
            unChangeCommonParams.put("pn", CashLoanApplication.getContext().getPackageName());
            // 设备型号
            unChangeCommonParams.put("bd", TextUtils.concat(DeviceUtils.getBrand(), " ",
                    DeviceUtils.getModel()).toString());
            unChangeCommonParams.put("osVersion", DeviceUtils.getSdkVersion());
            unChangeCommonParams.put("lg", Locale.getDefault().getLanguage());

            String media_source = Acache.get(CashLoanApplication.getContext()).getAsString(AcacheKeys.MEDIA_SOURCE);
            if (!TextUtils.isEmpty(media_source)) {
                unChangeCommonParams.put("ms", media_source);
            }else {
                unChangeCommonParams.put("ms", "organic");
                unChangeCommonParams.put("device_id", UniqueUtils.getUniquePsuedoID());
                unChangeCommonParams.put("ad", AppsFlyerLib.getInstance().getAppsFlyerUID(CashLoanApplication.getContext()));
            }

        }
    }

    private static void checkInstallParams() {
        if (installCommonParams == null || installCommonParams.size() == 0) {
            installCommonParams = new HashMap<>();
            // 客户端软件版本
            installCommonParams.put("cv", DeviceUtils
                    .getVersionNameAndVersionCode(CashLoanApplication.getContext()));
            // 操作系统
            installCommonParams.put("os", DeviceUtils.getPlatform());
            // 操作系统版本
            installCommonParams.put("ov", Build.VERSION.RELEASE);
            // 屏幕尺寸
            installCommonParams.put("sc", ScreenUtils.genScreenResolution());
            // 包名
            installCommonParams.put("pn", CashLoanApplication.getContext().getPackageName());

            // 设备型号
            installCommonParams.put("bd", TextUtils.concat(DeviceUtils.getBrand(), " ",
                    DeviceUtils.getModel()).toString());
            installCommonParams.put("osVersion", DeviceUtils.getSdkVersion());
            installCommonParams.put("lg", Locale.getDefault().getLanguage());
            String media_source = Acache.get(CashLoanApplication.getContext()).getAsString(AcacheKeys.MEDIA_SOURCE);
            if (!TextUtils.isEmpty(media_source)) {
                installCommonParams.put("ms", media_source);
            }else {
                installCommonParams.put("ms", "organic");
            }

        }
    }

}
